// 投票
const voteMixin = {
	data(){
		return{
			//投票Id
			voteId: '',
			//投票面板
			isVote: false,
			//投票信息
			voteInfo: [],
			//投票剩余时间计数
			currentTime: 0,
			//未投票的选项id
			isMissId: '',
			// 投票定时
			voteTimer: '',
			//投票时长
			voteTime: 40,
		}
	},
	watch: {
		isVote(e){
			if(e){
				this.currentTime = this.voteTime
				this.voteTimer = setInterval(()=>{
					//投票中,计时文本更新,计时结束，关闭投票面板
					if(this.currentTime <= 0){
						this.voteExec(this.isMissId, '没选')
						this.isVote = false
					}else{
						this.currentTime --
					}
				}, 1000)
			}else{
				clearInterval(this.voteTimer)
			}
		}
	},
	onUnload() {
		clearInterval(this.voteTimer);
	},
	onHide() {
		clearInterval(this.voteTimer);
	},
	methods: {
		// 接收投票数据
		getVote(data){
			this.voteId = data.voteId;
			this.voteInfo = data.itemList;
			this.isMissId = this.voteInfo.filter(res=>res.isMiss)[0].id;
			if(!this.isVote){
				this.isVote = true;
			}
		},
		//投票
		voteExec(selected_id, sletected_name){
			this.$http.request({
				url: 'api/LivePlayer/CreateVoter',
				allow: true,
				showLoading: false,
				method: 'PUT',
				data: {
					voteId: this.voteId, 
					voteItemId: selected_id,
					mainId: this.classId,
					itemId: this.Id,
					voteItemName: sletected_name
				}
			}).then(resp=>{
				this.socket.sendSocketMessage('0002', {
					voteId: this.voteId,
					voteItemId: selected_id,
					classId: this.classId,
					id: this.Id
				})
			}).finally(_=>{
				this.isVote = false
			})
		},
	}
}

export default voteMixin;